package com.wkw.jkyf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkw.jkyf.common.ResultMapUtil;
import com.wkw.jkyf.pojo.Saleinfo;

import com.wkw.jkyf.service.ISaleInfoService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping(value = "/saleinfo")
public class SaleinfoController {
    @Resource
    private ISaleInfoService saleinfoService;

    @RequestMapping
    public String login(){
        return "/saleinfo";
    }

    /*
    分页查询供应商列表
     */
    @RequestMapping("/saleinfoQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize){
        try{
            IPage<Saleinfo> iPage = saleinfoService.selectSaleinfoPage(page,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtil.getHashException(e);
        }

    }
    //跳转到添加页面
    @RequestMapping(value = "/saleinfoPage")
    public String supplierPage(){
        return "/saleinfoPage";
    }

    //添加
    @RequestMapping(value = "/saleinfoAdd")
    @ResponseBody
    public Object saleinfoAdd(Saleinfo saleinfo){
        try {
        //    saleinfo.setOperatetime(new Date());
            int i = saleinfoService.addSaleinfo(saleinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashException(e);
        }

    }

    //按id查询
    @RequestMapping(value = "/saleinfoQueryById")
    public String saleinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model) {
        Saleinfo saleinfo= saleinfoService.querySaleinfoById(id);
        model.addAttribute("obj",saleinfo);
        return "/saleinfoPage";
    }

    //修改一条销售记录
    @RequestMapping(value = "/saleinfoEdit")
    @ResponseBody
    public Object saleinfoEdit(Saleinfo saleinfo) {
        try {
            int i= saleinfoService.editSaleinfo(saleinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }

    // 删除一个销售记录
    @RequestMapping(value = "/saleinfoDelById")
    @ResponseBody
    public Object saleinfoDelById(Integer id) {
        try {
            int i= saleinfoService.deleteSaleinfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }

}
